/** \file    mswin/event.h
  * \brief   synchronization object that permits to one thread notify other threads waiing for some event.
  * \date    2003 - 2004
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/include/mswin/event.h,v 1.3 2004/05/03 18:01:40 cscom1r4 Exp $
  */

#ifndef mswin_event_h
#define mswin_event_h

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

namespace mswin {

struct event {
	HANDLE h;
public:
	event(bool awake_maximum_one_thread_when_set);
	~event();
	void set();
	void reset();
	void pulse();
	void wait() const;
	bool is_set() const;
	/* The difference between 
	   a) awake_maximum_one_thread_when_set = true followed by pulse()  and
	   b) awake_maximum_one_thread_when_set = false followed by pulse()
	   In the case a) maximum one waiting thread is resumed, in case b) all waiting threads are resumed.
	*/
private: //prohibit copy constructors
	event(const event &);
	event & operator = (const event &);
};

//inlines

inline event::event(bool awake_maximum_one_thread_when_set)
	{ h = CreateEvent(0, awake_maximum_one_thread_when_set ? FALSE : TRUE, FALSE, 0); }

inline event::~event()
	{ CloseHandle(h); }

inline void event::set()
	{ SetEvent(h); }

inline void event::reset()
	{ ResetEvent(h); }

inline void event::pulse()
	{ PulseEvent(h); }

inline void event::wait() const
	{ WaitForSingleObject(h, INFINITE); }

inline bool event::is_set() const
	{ return WaitForSingleObject(h, 0) == WAIT_OBJECT_0; }

} // namespace mswin

#endif //mswin_event_h
